/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sack.sackclipboard;



import java.io.Serializable;

import java.util.Vector;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.text.JTextComponent;



import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.Utilities;

/**
 * Top component which displays something.
 */
final class BoardTopComponent extends TopComponent {

 JTextComponent editor;
int i=0;
private	Vector	listData=new Vector();;
  String str="hello";



    private static BoardTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";

    private static final String PREFERRED_ID = "BoardTopComponent";
    private String Sel="";
    private int index;

    private BoardTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(BoardTopComponent.class, "CTL_BoardTopComponent"));
        setToolTipText(NbBundle.getMessage(BoardTopComponent.class, "HINT_BoardTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        Cut = new javax.swing.JButton();
        Copy = new javax.swing.JButton();
        Paste = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        Cut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sack/sackclipboard/cut.GIF"))); // NOI18N
        Cut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CutActionPerformed(evt);
            }
        });

        Copy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sack/sackclipboard/copy.GIF"))); // NOI18N
        Copy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CopyActionPerformed(evt);
            }
        });

        Paste.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sack/sackclipboard/paste.GIF"))); // NOI18N
        Paste.setEnabled(false);
        Paste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PasteActionPerformed(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(240, 240, 240));
        jLabel1.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 14));
        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "Visual ClipBoard");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sack/sackclipboard/remove.GIF"))); // NOI18N
        jButton1.setEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(Cut)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(Copy)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(Paste)
                        .add(10, 10, 10)
                        .add(jButton1))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(116, 116, 116)
                        .add(jLabel1)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 18, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton1)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(Cut)
                        .add(Copy)
                        .add(Paste)))
                .addContainerGap())
        );

        jButton1.getAccessibleContext().setAccessibleName("remove");

        jList1.setFont(new java.awt.Font("Monospaced", 0, 14));
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void CopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CopyActionPerformed
   
     
         try{
      str = org.netbeans.api.editor.EditorRegistry.lastFocusedComponent().getSelectedText();
  
        System.out.println(str);
        listData.addElement(str);
      
jList1.setListData(listData);
jScrollPane1.revalidate();
				jScrollPane1.repaint();
        Paste.setEnabled(true);
        jButton1.setEnabled(true);
         
         
         }
 catch(NullPointerException NPE)
  {
 Object[] options = {"OK"};
JOptionPane.showOptionDialog(null, "NO TEXT SELECTED", "Warning",

            JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,

            null, options, options[0]);
  }
   
       
}//GEN-LAST:event_CopyActionPerformed

    private void CutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CutActionPerformed
    try{
      str = org.netbeans.api.editor.EditorRegistry.lastFocusedComponent().getSelectedText();
  
        System.out.println(str);
        listData.addElement(str);
      
jList1.setListData(listData);
jScrollPane1.revalidate();
				jScrollPane1.repaint();
        Paste.setEnabled(true);
        jButton1.setEnabled(true);
         
          org.netbeans.api.editor.EditorRegistry.lastFocusedComponent().replaceSelection("");
         }
 catch(NullPointerException NPE)
  {
 Object[] options = {"OK"};
JOptionPane.showOptionDialog(null, "NO TEXT SELECTED", "Warning",

            JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,

            null, options, options[0]);
  }
   
    }//GEN-LAST:event_CutActionPerformed

    private void PasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PasteActionPerformed
        // TODO add your handling code here:
       

       org.netbeans.api.editor.EditorRegistry.lastFocusedComponent().replaceSelection(Sel);
    }//GEN-LAST:event_PasteActionPerformed

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        // TODO add your handling code here:
        Sel=(String)jList1.getSelectedValue();
        index=jList1.getSelectedIndex();
    }//GEN-LAST:event_jList1ValueChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
if(index!=-1){
    listData.removeElementAt( index );

       
         jList1.setListData(listData);
            jScrollPane1.revalidate();
            jScrollPane1.repaint();
           
            if(listData.size()==0)
                                {
                                    jButton1.setEnabled(false);
                                    Paste.setEnabled(false);
                                }
}
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Copy;
    private javax.swing.JButton Cut;
    private javax.swing.JButton Paste;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
  
    public static synchronized BoardTopComponent getDefault() {
        if (instance == null) {
            instance = new BoardTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the BoardTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized BoardTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(BoardTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof BoardTopComponent) {
            return (BoardTopComponent) win;
        }
        Logger.getLogger(BoardTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
    // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
    // TODO add custom code on component closing
    }

    /** replaces this in object stream */
    @Override
    public Object writeReplace() {
        return new ResolvableHelper();
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    final static class ResolvableHelper implements Serializable {

        private static final long serialVersionUID = 1L;

        public Object readResolve() {
            return BoardTopComponent.getDefault();
        }
    }
}
